package com.apex.flink.dialect;

import com.apex.flink.rowconverter.HiveJdbcRowConverter;
import org.apache.flink.connector.jdbc.dialect.JdbcDialect;
import org.apache.flink.connector.jdbc.internal.converter.JdbcRowConverter;
import org.apache.flink.table.types.logical.RowType;

import java.util.Optional;

public class HiveDialect implements JdbcDialect {
    private static final long serialVersionUID = 1L;
    @Override
    public String dialectName() {
        return "hive2";
    }

    @Override
    public boolean canHandle(String url) {
        return url.startsWith("jdbc:hive2:");
    }

    @Override
    public JdbcRowConverter getRowConverter(RowType rowType) {
        return new HiveJdbcRowConverter(rowType);
    }

    @Override
    public String getLimitClause(long limit) {
        return "LIMIT " + limit;
    }

    @Override
    public Optional<String> defaultDriverName() {
        return Optional.of("com.cloudera.hive.jdbc41.HS2Driver");
    }

    @Override
    public String quoteIdentifier(String identifier) {
        return "`" + identifier + "`";
    }
}
